<template>
  <view class="container">
    <view class="header">
      <view class="back-btn" @click="goBack">
        <uni-icons type="back" size="24"></uni-icons>
      </view>
      <view class="title">校园食堂</view>
    </view>
    
    <!-- 食堂列表 -->
    <view class="canteen-list">
      <view class="canteen-card" 
        v-for="(item, index) in canteenList" 
        :key="index"
        @click="goToCanteenDetail(item.id)"
      >
        <image class="canteen-bg" :src="item.image" mode="aspectFill"></image>
        <view class="canteen-overlay"></view>
        <view class="canteen-info">
          <view class="canteen-title">
            <view class="canteen-name">{{ item.name }}</view>
            <view class="canteen-floor">{{ item.floor }}</view>
          </view>
          <view class="canteen-meta">
            <view class="canteen-count">{{ item.merchantCount }}家商户</view>
            <uni-icons type="right" size="16" color="#fff"></uni-icons>
          </view>
        </view>
      </view>
    </view>
    
    <!-- 加载更多区域 -->
    <view class="load-more-area">
      <uni-load-more :status="loadingStatus" :contentText="{
        contentdown: '上拉显示更多',
        contentrefresh: '正在加载...',
        contentnomore: '我是有底线的'
      }"></uni-load-more>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { getCanteenPage } from '@/api/canteen.js';
import uniIcons from '@/uni_modules/uni-icons/components/uni-icons/uni-icons.vue';
import uniLoadMore from '@/uni_modules/uni-load-more/components/uni-load-more/uni-load-more.vue';

const pageSize = 10; // 每页显示的食堂数量
const currentPage = ref(1); // 当前页码
const loadingStatus = ref('more'); // 加载状态：more-加载前，loading-加载中，noMore-没有更多了
const canteenList = ref([]); // 食堂列表
const total = ref(0); // 总数量

// 返回上一页
const goBack = () => {
  uni.navigateBack();
};

// 跳转到食堂详情页
const goToCanteenDetail = (canteenId) => {
  uni.navigateTo({
    url: `/pages/canteen/detail?id=${canteenId}`
  });
};

// 加载食堂列表
const loadCanteenList = async () => {
  if (loadingStatus.value === 'loading' || loadingStatus.value === 'noMore') {
    return;
  }

  loadingStatus.value = 'loading';

  try {
    const res = await getCanteenPage({
      pageNo: currentPage.value,
      pageSize: pageSize
    });

    const { list, total: totalCount } = res.data;
    
    // 添加到已加载的食堂列表中
    canteenList.value = [...canteenList.value, ...list];
    total.value = totalCount;
    
    // 更新页码和加载状态
    currentPage.value++;
    
    // 检查是否还有更多数据
    if (canteenList.value.length >= totalCount) {
      loadingStatus.value = 'noMore';
    } else {
      loadingStatus.value = 'more';
    }
  } catch (error) {
    console.error('加载食堂列表失败:', error);
    loadingStatus.value = 'more';
    uni.showToast({
      title: '加载失败，请重试',
      icon: 'none'
    });
  }
};

// 页面加载时的初始化操作
onMounted(() => {
  loadCanteenList();
});

// 页面上拉触底事件的处理函数
const onReachBottom = () => {
  loadCanteenList();
};

// 监听页面滚动到底部事件
uni.$on('onReachBottom', onReachBottom);
</script>

<script>
export default {
  // 页面上拉触底事件的处理函数
  onReachBottom() {
    uni.$emit('onReachBottom');
  }
}
</script>

<style lang="scss" scoped>
.container {
  min-height: 100vh;
  background: #f0f2f5;
  padding-bottom: 30rpx;
}

.header {
  display: flex;
  align-items: center;
  height: 90rpx;
  background: #fff;
  padding: 0 30rpx;
  position: relative;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.back-btn {
  position: absolute;
  left: 30rpx;
  top: 50%;
  transform: translateY(-50%);
  z-index: 10;
}

.title {
  flex: 1;
  text-align: center;
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.canteen-list {
  padding: 30rpx;
}

.canteen-card {
  height: 240rpx;
  border-radius: 20rpx;
  overflow: hidden;
  position: relative;
  margin-bottom: 30rpx;
  box-shadow: 0 6rpx 20rpx rgba(0, 0, 0, 0.1);
}

.canteen-bg {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.canteen-overlay {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(to bottom, rgba(0, 0, 0, 0.2), rgba(0, 0, 0, 0.6));
}

.canteen-info {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  padding: 30rpx;
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
}

.canteen-name {
  font-size: 36rpx;
  font-weight: bold;
  color: #fff;
  margin-bottom: 10rpx;
  text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.5);
}

.canteen-floor {
  font-size: 28rpx;
  color: #fff;
  opacity: 0.9;
}

.canteen-meta {
  display: flex;
  align-items: center;
  gap: 10rpx;
}

.canteen-count {
  font-size: 24rpx;
  color: #fff;
  padding: 6rpx 16rpx;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 20rpx;
  backdrop-filter: blur(4px);
}
</style>
